#ifndef WINDOWSFRAMETIMER_H
#define WINDOWSFRAMETIMER_H

#pragma once

#include <LDF/FrameTimer.h>
#include "../ToolkitLayer.h"


/**
 * This is a MS Windows implemetation of the FrameTimer class, used to caclulate the frame rate when rendering components in the UofC Large Display Framework.
 *
 * This class measures the time spent during a single rendering pass and stores the value of this time "interval" within the #intervals queue.
 * The average of all elements in the queue thereby provides a relatively smoothed FPS measurement.
 *
 * This implementation uses the GetTickCount() function to obtain the number of clock ticks (actually, the
 * number of milliseconds) passed since the system was booted (up to 49.7 days)
 * and converts it to seconds using the CLOCKS_PER_SEC constant. 
 *
 * This implementation was based on the frame rate calculations used in the
 * TabletopVis class of the original implementation of the UofC Tabletop
 * Framework by A. Miede and T. Isenberg in the Interactions Lab at the
 * University of Calgary, Canada.
 *
 * @author Fabricio Anastacio - fabriciocfa@yahoo.com
 * @since July, 26, 2007
 */
class TOOLKITLAYER_API WindowsFrameTimer : public FrameTimer
{
	// Attributes
private:
	/** The start time of the interval being measured. */
	unsigned long startTime;
	/** The end time of the interval being measured. */
	unsigned long endTime;
	/** The average of the intervals being measured. */
	double averageInterval;
	/** The maximum number of intervals to be considered. */
	unsigned int maxIntervals;
	/** The sum of all interval values. */
	unsigned long sumIntervals;
	/** The queue of interval values used to calculate the average value. */
	std::queue<unsigned long> intervals;

	// Methods
public:

	/**
	 * Constructs this Windows frame timer object. If no value is given to the maximum
	 * number of intervals, a defualt value of 10 is used.
	 *
	 * @param maxNumberIntervals the value to be set as the maximum number of
	 * intervals in the average calcualtions.
	 */
	WindowsFrameTimer(unsigned int maxNumberIntervals = 10);
	
	/**
	 * Destroys this Windows frame timer object.
	 */
	~WindowsFrameTimer(void);

	/**
	 * Starts measuring an interval. This is the implementation of the pure virtual function in the abstract FrameTimer class.
	 */
	void start();
	
	/**
	 * Ends measuring an interval. This is the implementation of the pure virtual function in the abstract FrameTimer class.
	 *
	 * This method adds the value of the recently concluded interval to the #intervals queue and, updates the #sumIntervals and #averageInterval attributes.
	 */
	void end();
	
	/**
	 * Calculates the frame per seconds value, using the value of the #averageInterval attribute.
	 *
	 * @return the fps value based on the current interval values.
	 */
	double calculateFrameRate();
};

#endif // WINDOWSFRAMETIMER_H
